<!DOCTYPE html><html lang="zh-tw"><head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Class&#x548C;Module | ECMAScript 6&#x5165;&#x95E8;</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.4.3">
        <meta name="author" content="&#x5927;&#x9F3B;&#x5B50;">
        
        <meta name="HandheldFriendly" content="true">
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://wohugb.gitbooks.io/ecmascript-6/content/gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="https://gstatic.gitbook.com/images/b1c4c86ca817a71628d2debe73be9a64.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="static/css/style.css">
    
        
        <link rel="stylesheet" href="static/css/website.css">
        
    
    

        
    
    
    <link rel="next" href="reference.html">
    
    
    <link rel="prev" href="promise.html">
    

        
    
<style>
    .book .book-summary .book-search,
    .book .book-summary ul.summary {
        position: relative;
        top: auto;
        left: auto;
        right: auto;
        bottom: auto;
    }
    .book-summary {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        flex-direction: column;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        overflow-y: auto;
    }
    .book-summary > nav {
        flex: 1 1 auto;
        overflow-y: auto;
        overflow-y: overlay;
    }
    .gl-wrapper {
        margin: 0;
        display: block;
        padding: 0;
        position: relative;
        line-height: 15px;
        background: #fafafa;
    }
    .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
        background: -moz-linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
        top: -24px;
        width: 100%;
        height: 24px;
        content: "";
        display: block;
        position: absolute;
        background: linear-gradient(to bottom, rgba(250,250,250,0) 0%, #fafafa 100%);
    }
    .book.color-theme-1 .gl-wrapper {
        background: #121212;
    }
    .book.color-theme-1 .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
        background: -moz-linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
        background: linear-gradient(to bottom, rgba(18,18,18,0) 0%, #121212 100%);
    }
    .book.color-theme-2 .gl-wrapper {
        background: #2e3243;
    }
    .book.color-theme-2 .gl-wrapper:before {
        background: -webkit-linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
        background: -moz-linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
        background: linear-gradient(to bottom, rgba(46,50,67,0) 0%, #2e3243 100%);
    }
    .gl-wrapper .gitbook-link {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        color: #9DAAB6;
        margin: 16px;
        display: flex;
        padding: 0;
        align-items: center;
        margin-left: 32px;
        padding-top: 8px;
        padding-left: 16px;
        border-radius: 3px;
        padding-right: 16px;
        padding-bottom: 8px;
        text-decoration: none;
        background-color: #E6ECF1;
        -webkit-box-align: center;
    }
    .gl-wrapper .tm-logo {
        margin: 0;
        display: block;
        padding: 0;
        font-size: 40px;
    }
    .gl-wrapper .tm-content {
        flex: 1;
        margin: 0;
        display: block;
        padding: 0;
        padding-left: 16px;
    }
    .gl-wrapper .tm-content .container {
        margin: 0;
        display: block;
        padding: 0;
    }
    .gl-wrapper .tm-content .tm-container .tm-text {
        font-size: 12px;
        font-family: "Roboto", sans-serif;
        font-weight: 400;
        line-height: 1.625;
    }
</style></head>
    <body>
        
        
    <div class="book" data-level="13" data-basepath=".." data-revision="Sun Oct 25 2015 02:52:03 GMT+0000 (UTC)">
    

<div class="book-summary">
    <div class="book-search" role="search">
        <input type="text" placeholder="&#x8F38;&#x5165;&#x4E26;&#x641C;&#x5C0B;" class="form-control">
    </div>
    <nav role="navigation">
        <ul class="summary">
            
            
            
                
                <li>
                    <a href="javascript:;" target="blank" class="custom-link">ECMAScript 6&#x5165;&#x95E8;</a>
                </li>
            
            

            
            <li class="divider"></li>
            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="index.html">
                
                        <i class="fa fa-check"></i>
                        
                        &#x524D;&#x8A00;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/intro.html">
            
                
                    <a href="intro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        &#x7B80;&#x4ECB;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/let.html">
            
                
                    <a href="let.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        let&#x548C;const&#x547D;&#x4EE4;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/destructuring.html">
            
                
                    <a href="destructuring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        &#x53D8;&#x91CF;&#x7684;&#x89E3;&#x6784;&#x8D4B;&#x503C;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/string.html">
            
                
                    <a href="string.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        &#x5B57;&#x7B26;&#x4E32;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/number.html">
            
                
                    <a href="number.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        &#x6570;&#x503C;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/array.html">
            
                
                    <a href="array.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        &#x6570;&#x7EC4;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/object.html">
            
                
                    <a href="object.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        &#x5BF9;&#x8C61;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/function.html">
            
                
                    <a href="function.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        &#x51FD;&#x6570;&#x7684;&#x6269;&#x5C55;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/set-map.html">
            
                
                    <a href="set-map.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        Set&#x548C;Map&#x6570;&#x636E;&#x7ED3;&#x6784;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="docs/iterator.html">
            
                
                    <a href="iterator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Iterator&#x548C;for...of&#x5FAA;&#x73AF;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="docs/generator.html">
            
                
                    <a href="generator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        Generator &#x51FD;&#x6570;
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="docs/promise.html">
            
                
                    <a href="promise.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        Promise&#x5BF9;&#x8C61;
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="13" data-path="docs/class.html">
            
                
                    <a href="">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        Class&#x548C;Module
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="docs/reference.html">
            
                
                    <a href="reference.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        &#x53C2;&#x8003;&#x94FE;&#x63A5;
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            
            
        </ul>
    </nav>

<div class="gl-wrapper">
    <a class="gitbook-link" href="javascript:;" target="blank">
        <div class="tm-logo">
            <svg preserveaspectratio="xMidYMid meet" height="1em" width="1em" fill="currentColor" viewbox="0 0 1067 769" xmlns="http://www.w3.org/2000/svg" stroke="none" class="icon-7f6730be--text-3f89f380"><g><path d="M480.026 640.677c17.205 0 31.2 13.997 31.2 31.194s-13.995 31.193-31.2 31.193c-17.197 0-31.193-13.996-31.193-31.193 0-17.197 13.996-31.194 31.193-31.194m489.93-193.226c-17.203 0-31.2-13.998-31.2-31.195 0-17.204 13.997-31.2 31.2-31.2 17.198 0 31.194 13.996 31.194 31.2 0 17.197-13.996 31.195-31.193 31.195m0-127.804c-53.269 0-96.609 43.34-96.609 96.609 0 10.373 1.723 20.702 5.123 30.741L559.328 616.879c-18.132-26.128-47.521-41.617-79.302-41.617-36.821 0-70.391 21.065-86.63 54.003L106.68 478.109c-30.288-15.927-52.965-65.817-50.56-111.223 1.248-23.687 9.438-42.071 21.897-49.17 7.916-4.493 17.436-4.099 27.526 1.188l1.916 1.01c75.96 40.022 324.6 170.981 335.063 175.844 16.157 7.47 25.14 10.5 52.659-2.547l513.958-267.3c7.53-2.844 16.315-10.062 16.315-21.023 0-15.205-15.72-21.199-15.765-21.199-29.218-14.018-74.163-35.054-117.987-55.57C798.033 84.26 691.861 34.547 645.23 10.132c-40.253-21.072-72.655-3.311-78.432.282l-11.227 5.555C345.727 119.743 64.898 258.826 48.911 268.553 20.278 285.973 2.547 320.679.252 363.768c-3.586 68.304 31.261 139.506 81.069 165.634l303.172 156.354c6.83 47.306 47.55 82.725 95.532 82.725 52.78 0 95.808-42.546 96.603-95.14L910.541 492.38c16.93 13.233 37.92 20.486 59.416 20.486 53.268 0 96.61-43.341 96.61-96.61s-43.342-96.61-96.61-96.61" fill-rule="evenodd"></path></g></svg>
        </div>
        <div class="tm-content">
            <div class="tm-container">
                <span class="tm-text">
                    Powered by <b>GitBook</b>
                </span>
            </div>
        </div>
    </a>
</div></div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="&#x76EE;&#x9304;"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="&#x641C;&#x5C0B;"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="&#x5B57;&#x578B;&#x8A2D;&#x5B9A;"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">&#x896F;&#x7DDA;&#x9AD4;</button>
        <button type="button" data-font="1" class="button">&#x7121;&#x896F;&#x7DDA;&#x9AD4;</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">&#x767D;&#x8272;</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">&#x68D5;&#x8910;&#x8272;</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">&#x591C;&#x9593;</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="&#x5206;&#x4EAB;"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    &#x5206;&#x4EAB;&#x5230; Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    &#x5206;&#x4EAB;&#x5230; Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    &#x5206;&#x4EAB;&#x5230; Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    &#x5206;&#x4EAB;&#x5230; Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    &#x5206;&#x4EAB;&#x5230; Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="index.html">ECMAScript 6&#x5165;&#x95E8;</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="class%E5%92%8Cmodule">Class&#x548C;Module</h1>
<h2 id="class">Class</h2>
<p>ES5&#x901A;&#x8FC7;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x5B9A;&#x4E49;&#x5E76;&#x751F;&#x6210;&#x65B0;&#x5BF9;&#x8C61;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Point</span>(<span class="hljs-params">x,y</span>)</span>{
    <span class="hljs-keyword">this</span>.x = x;
    <span class="hljs-keyword">this</span>.y = y;
}

Point.prototype.toString = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;(&apos;</span>+<span class="hljs-keyword">this</span>.x+<span class="hljs-string">&apos;, &apos;</span>+<span class="hljs-keyword">this</span>.y+<span class="hljs-string">&apos;)&apos;</span>;
}
</code></pre>
<p>ES6&#x5F15;&#x5165;&#x4E86;Class&#xFF08;&#x7C7B;&#xFF09;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#xFF0C;&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x6A21;&#x677F;&#x3002;&#x901A;&#x8FC7;class&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x7C7B;&#x3002;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7528;&#x201C;&#x7C7B;&#x201D;&#x6539;&#x5199;&#xFF0C;&#x5C31;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">//&#x5B9A;&#x4E49;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Point</span> </span>{

  constructor(x, y) {
    <span class="hljs-keyword">this</span>.x = x;
    <span class="hljs-keyword">this</span>.y = y;
  }

  toString() {
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;(&apos;</span>+<span class="hljs-keyword">this</span>.x+<span class="hljs-string">&apos;, &apos;</span>+<span class="hljs-keyword">this</span>.y+<span class="hljs-string">&apos;)&apos;</span>;
  }

}

<span class="hljs-keyword">var</span> point = <span class="hljs-keyword">new</span> Point(<span class="hljs-number">2</span>,<span class="hljs-number">3</span>);
point.toString() <span class="hljs-comment">// (2, 3)</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x201C;&#x7C7B;&#x201D;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x91CC;&#x9762;&#x6709;&#x4E00;&#x4E2A;constructor&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x800C;this&#x5173;&#x952E;&#x5B57;&#x5219;&#x4EE3;&#x8868;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x4E2A;&#x7C7B;&#x9664;&#x4E86;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD8;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;toString&#x65B9;&#x6CD5;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x524D;&#x9762;&#x4E0D;&#x9700;&#x8981;&#x52A0;&#x4E0A;function&#x8FD9;&#x4E2A;&#x4FDD;&#x7559;&#x5B57;&#xFF0C;&#x76F4;&#x63A5;&#x628A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x653E;&#x8FDB;&#x53BB;&#x4E86;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</p>
<p>Class&#x4E4B;&#x95F4;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;extends&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5B9E;&#x73B0;&#x7EE7;&#x627F;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ColorPoint</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Point</span> </span>{}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;ColorPoint&#x7C7B;&#xFF0C;&#x8BE5;&#x7C7B;&#x901A;&#x8FC7;extends&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x7EE7;&#x627F;&#x4E86;Point&#x7C7B;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;&#x4F46;&#x662F;&#x7531;&#x4E8E;&#x6CA1;&#x6709;&#x90E8;&#x7F72;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x5B8C;&#x5168;&#x4E00;&#x6837;&#xFF0C;&#x7B49;&#x4E8E;&#x590D;&#x5236;&#x4E86;&#x4E00;&#x4E2A;Point&#x7C7B;&#x3002;&#x4E0B;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x5728;ColorPoint&#x5185;&#x90E8;&#x52A0;&#x4E0A;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ColorPoint</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Point</span> </span>{

  constructor(x, y, color) {
    <span class="hljs-keyword">super</span>(x, y); <span class="hljs-comment">// &#x7B49;&#x540C;&#x4E8E;super.constructor(x, y)</span>
    <span class="hljs-keyword">this</span>.color = color;
  }

  toString() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.color+<span class="hljs-string">&apos; &apos;</span>+<span class="hljs-keyword">super</span>();
  }

}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;constructor&#x65B9;&#x6CD5;&#x548C;toString&#x65B9;&#x6CD5;&#x4E4B;&#x4E2D;&#xFF0C;&#x90FD;&#x51FA;&#x73B0;&#x4E86;super&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5B83;&#x6307;&#x4EE3;&#x7236;&#x7C7B;&#x7684;&#x540C;&#x540D;&#x65B9;&#x6CD5;&#x3002;&#x5728;constructor&#x65B9;&#x6CD5;&#x5185;&#xFF0C;super&#x6307;&#x4EE3;&#x7236;&#x7C7B;&#x7684;constructor&#x65B9;&#x6CD5;&#xFF1B;&#x5728;toString&#x65B9;&#x6CD5;&#x5185;&#xFF0C;super&#x6307;&#x4EE3;&#x7236;&#x7C7B;&#x7684;toString&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x6709;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#xFF0C;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x3002;&#x7C7B;&#x548C;&#x6A21;&#x5757;&#x7684;&#x5185;&#x90E8;&#xFF0C;&#x9ED8;&#x8BA4;&#x5C31;&#x662F;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>use strict</code>&#x6307;&#x5B9A;&#x8FD0;&#x884C;&#x6A21;&#x5F0F;&#x3002;&#x8003;&#x8651;&#x5230;&#x672A;&#x6765;&#x6240;&#x6709;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5176;&#x5B9E;&#x90FD;&#x662F;&#x8FD0;&#x884C;&#x5728;&#x6A21;&#x5757;&#x4E4B;&#x4E2D;&#xFF0C;&#x6240;&#x4EE5;ES6&#x5B9E;&#x9645;&#x4E0A;&#x628A;&#x6574;&#x4E2A;&#x8BED;&#x8A00;&#x5347;&#x7EA7;&#x5230;&#x4E86;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x3002;</p>
<h2 id="module%E7%9A%84%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95">Module&#x7684;&#x57FA;&#x672C;&#x7528;&#x6CD5;</h2>
<p>JavaScript&#x6CA1;&#x6709;&#x6A21;&#x5757;&#xFF08;module&#xFF09;&#x4F53;&#x7CFB;&#xFF0C;&#x65E0;&#x6CD5;&#x5C06;&#x4E00;&#x4E2A;&#x5927;&#x7A0B;&#x5E8F;&#x62C6;&#x5206;&#x6210;&#x4E92;&#x76F8;&#x4F9D;&#x8D56;&#x7684;&#x5C0F;&#x6587;&#x4EF6;&#xFF0C;&#x518D;&#x7528;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x62FC;&#x88C5;&#x8D77;&#x6765;&#x3002;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x90FD;&#x6709;&#x8FD9;&#x9879;&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982;Ruby&#x7684;require&#x3001;Python&#x7684;import&#xFF0C;&#x751A;&#x81F3;&#x5C31;&#x8FDE;CSS&#x90FD;&#x6709;@import&#xFF0C;&#x4F46;&#x662F;JavaScript&#x4EFB;&#x4F55;&#x8FD9;&#x65B9;&#x9762;&#x7684;&#x652F;&#x6301;&#x90FD;&#x6CA1;&#x6709;&#xFF0C;&#x8FD9;&#x5BF9;&#x5F00;&#x53D1;&#x5927;&#x578B;&#x7684;&#x3001;&#x590D;&#x6742;&#x7684;&#x9879;&#x76EE;&#x5F62;&#x6210;&#x4E86;&#x5DE8;&#x5927;&#x969C;&#x788D;&#x3002;</p>
<p>&#x5728;ES6&#x4E4B;&#x524D;&#xFF0C;&#x793E;&#x533A;&#x5236;&#x5B9A;&#x4E86;&#x4E00;&#x4E9B;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x65B9;&#x6848;&#xFF0C;&#x6700;&#x4E3B;&#x8981;&#x7684;&#x6709;CommonJS&#x548C;AMD&#x4E24;&#x79CD;&#x3002;&#x524D;&#x8005;&#x7528;&#x4E8E;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x540E;&#x8005;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#x3002;ES6&#x5728;&#x8BED;&#x8A00;&#x89C4;&#x683C;&#x7684;&#x5C42;&#x9762;&#x4E0A;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86;&#x6A21;&#x5757;&#x529F;&#x80FD;&#xFF0C;&#x800C;&#x4E14;&#x5B9E;&#x73B0;&#x5F97;&#x76F8;&#x5F53;&#x7B80;&#x5355;&#xFF0C;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x53D6;&#x4EE3;&#x73B0;&#x6709;&#x7684;CommonJS&#x548C;AMD&#x89C4;&#x8303;&#xFF0C;&#x6210;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x670D;&#x52A1;&#x5668;&#x901A;&#x7528;&#x7684;&#x6A21;&#x5757;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;</p>
<p>ES6&#x6A21;&#x5757;&#x7684;&#x8BBE;&#x8BA1;&#x601D;&#x60F3;&#xFF0C;&#x662F;&#x5C3D;&#x91CF;&#x7684;&#x9759;&#x6001;&#x5316;&#xFF0C;&#x4F7F;&#x5F97;&#x7F16;&#x8BD1;&#x65F6;&#x5C31;&#x80FD;&#x786E;&#x5B9A;&#x6A21;&#x5757;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x4EE5;&#x53CA;&#x8F93;&#x5165;&#x548C;&#x8F93;&#x51FA;&#x7684;&#x53D8;&#x91CF;&#x3002;CommonJS&#x548C;AMD&#x6A21;&#x5757;&#xFF0C;&#x90FD;&#x53EA;&#x80FD;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x786E;&#x5B9A;&#x8FD9;&#x4E9B;&#x4E1C;&#x897F;&#x3002;&#x6BD4;&#x5982;&#xFF0C;CommonJS&#x6A21;&#x5757;&#x5C31;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x8F93;&#x5165;&#x65F6;&#x5FC5;&#x987B;&#x67E5;&#x627E;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">var</span> { stat, exists, readFile } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
</code></pre>
<p>ES6&#x6A21;&#x5757;&#x4E0D;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7;export&#x547D;&#x4EE4;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x8F93;&#x51FA;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x8F93;&#x5165;&#x65F6;&#x4E5F;&#x91C7;&#x7528;&#x9759;&#x6001;&#x547D;&#x4EE4;&#x7684;&#x5F62;&#x5F0F;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">import</span> { stat, exists, readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;fs&apos;</span>;
</code></pre>
<p>&#x6240;&#x4EE5;&#xFF0C;ES6&#x53EF;&#x4EE5;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x5C31;&#x5B8C;&#x6210;&#x6A21;&#x5757;&#x7F16;&#x8BD1;&#xFF0C;&#x6548;&#x7387;&#x8981;&#x6BD4;CommonJS&#x6A21;&#x5757;&#x9AD8;&#x3002;</p>
<p><strong>&#xFF08;1&#xFF09;export&#x547D;&#x4EE4;&#xFF0C;import&#x547D;&#x4EE4;</strong></p>
<p>&#x6A21;&#x5757;&#x529F;&#x80FD;&#x4E3B;&#x8981;&#x7531;&#x4E24;&#x4E2A;&#x547D;&#x4EE4;&#x6784;&#x6210;&#xFF1A;export&#x548C;import&#x3002;export&#x547D;&#x4EE4;&#x7528;&#x4E8E;&#x7528;&#x6237;&#x81EA;&#x5B9A;&#x4E49;&#x6A21;&#x5757;&#xFF0C;&#x89C4;&#x5B9A;&#x5BF9;&#x5916;&#x63A5;&#x53E3;&#xFF1B;import&#x547D;&#x4EE4;&#x7528;&#x4E8E;&#x8F93;&#x5165;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x540C;&#x65F6;&#x521B;&#x9020;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF08;namespace&#xFF09;&#xFF0C;&#x9632;&#x6B62;&#x51FD;&#x6570;&#x540D;&#x51B2;&#x7A81;&#x3002;</p>
<p>ES6&#x5141;&#x8BB8;&#x5C06;&#x72EC;&#x7ACB;&#x7684;JS&#x6587;&#x4EF6;&#x4F5C;&#x4E3A;&#x6A21;&#x5757;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5141;&#x8BB8;&#x4E00;&#x4E2A;JavaScript&#x811A;&#x672C;&#x6587;&#x4EF6;&#x8C03;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x811A;&#x672C;&#x6587;&#x4EF6;&#x3002;&#x8BE5;&#x6587;&#x4EF6;&#x5185;&#x90E8;&#x7684;&#x6240;&#x6709;&#x53D8;&#x91CF;&#xFF0C;&#x5916;&#x90E8;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#xFF0C;&#x5FC5;&#x987B;&#x4F7F;&#x7528;export&#x5173;&#x952E;&#x5B57;&#x8F93;&#x51FA;&#x53D8;&#x91CF;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;JS&#x6587;&#x4EF6;&#xFF0C;&#x91CC;&#x9762;&#x4F7F;&#x7528;export&#x5173;&#x952E;&#x5B57;&#x8F93;&#x51FA;&#x53D8;&#x91CF;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// profile.js</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">var</span> firstName = <span class="hljs-string">&apos;Michael&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">var</span> lastName = <span class="hljs-string">&apos;Jackson&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">var</span> year = <span class="hljs-number">1958</span>;
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x662F;profile.js&#x6587;&#x4EF6;&#xFF0C;&#x4FDD;&#x5B58;&#x4E86;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x3002;ES6&#x5C06;&#x5176;&#x89C6;&#x4E3A;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x91CC;&#x9762;&#x7528;export&#x547D;&#x4EE4;&#x5BF9;&#x5916;&#x90E8;&#x8F93;&#x51FA;&#x4E86;&#x4E09;&#x4E2A;&#x53D8;&#x91CF;&#x3002;</p>
<p>export&#x7684;&#x5199;&#x6CD5;&#xFF0C;&#x9664;&#x4E86;&#x50CF;&#x4E0A;&#x9762;&#x8FD9;&#x6837;&#xFF0C;&#x8FD8;&#x6709;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// profile.js</span>
<span class="hljs-keyword">var</span> firstName = <span class="hljs-string">&apos;Michael&apos;</span>;
<span class="hljs-keyword">var</span> lastName = <span class="hljs-string">&apos;Jackson&apos;</span>;
<span class="hljs-keyword">var</span> year = <span class="hljs-number">1958</span>;

<span class="hljs-keyword">export</span> {firstName, lastName, year};
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5728;export&#x547D;&#x4EE4;&#x540E;&#x9762;&#xFF0C;&#x4F7F;&#x7528;&#x5927;&#x62EC;&#x53F7;&#x6307;&#x5B9A;&#x6240;&#x8981;&#x8F93;&#x51FA;&#x7684;&#x4E00;&#x7EC4;&#x53D8;&#x91CF;&#x3002;&#x5B83;&#x4E0E;&#x524D;&#x4E00;&#x79CD;&#x5199;&#x6CD5;&#xFF08;&#x76F4;&#x63A5;&#x653E;&#x7F6E;&#x5728;var&#x8BED;&#x53E5;&#x524D;&#xFF09;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5E94;&#x8BE5;&#x4F18;&#x5148;&#x8003;&#x8651;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x5199;&#x6CD5;&#x3002;&#x56E0;&#x4E3A;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x811A;&#x672C;&#x5C3E;&#x90E8;&#xFF0C;&#x4E00;&#x773C;&#x770B;&#x6E05;&#x695A;&#x8F93;&#x51FA;&#x4E86;&#x54EA;&#x4E9B;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x4F7F;&#x7528;export&#x547D;&#x4EE4;&#x5B9A;&#x4E49;&#x4E86;&#x6A21;&#x5757;&#x7684;&#x5BF9;&#x5916;&#x63A5;&#x53E3;&#x4EE5;&#x540E;&#xFF0C;&#x5176;&#x4ED6;JS&#x6587;&#x4EF6;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;import&#x547D;&#x4EE4;&#x52A0;&#x8F7D;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#xFF08;&#x6587;&#x4EF6;&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// main.js</span>

<span class="hljs-keyword">import</span> {firstName, lastName, year} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./profile&apos;</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sfirsetHeader</span>(<span class="hljs-params">element</span>) </span>{
  element.textContent = firstName + <span class="hljs-string">&apos; &apos;</span> + lastName;
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x5C5E;&#x4E8E;&#x53E6;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;main.js&#xFF0C;import&#x547D;&#x4EE4;&#x5C31;&#x7528;&#x4E8E;&#x52A0;&#x8F7D;profile.js&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x4ECE;&#x4E2D;&#x8F93;&#x5165;&#x53D8;&#x91CF;&#x3002;import&#x547D;&#x4EE4;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF08;&#x7528;&#x5927;&#x62EC;&#x53F7;&#x8868;&#x793A;&#xFF09;&#xFF0C;&#x91CC;&#x9762;&#x6307;&#x5B9A;&#x8981;&#x4ECE;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x5BFC;&#x5165;&#x7684;&#x53D8;&#x91CF;&#x540D;&#x3002;&#x5927;&#x62EC;&#x53F7;&#x91CC;&#x9762;&#x7684;&#x53D8;&#x91CF;&#x540D;&#xFF0C;&#x5FC5;&#x987B;&#x4E0E;&#x88AB;&#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF08;profile.js&#xFF09;&#x5BF9;&#x5916;&#x63A5;&#x53E3;&#x7684;&#x540D;&#x79F0;&#x76F8;&#x540C;&#x3002;</p>
<p>&#x5982;&#x679C;&#x60F3;&#x4E3A;&#x8F93;&#x5165;&#x7684;&#x53D8;&#x91CF;&#x91CD;&#x65B0;&#x53D6;&#x4E00;&#x4E2A;&#x540D;&#x5B57;&#xFF0C;import&#x8BED;&#x53E5;&#x4E2D;&#x8981;&#x4F7F;&#x7528;as&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5C06;&#x8F93;&#x5165;&#x7684;&#x53D8;&#x91CF;&#x91CD;&#x547D;&#x540D;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">import</span> { lastName <span class="hljs-keyword">as</span> surname } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./profile&apos;</span>;
</code></pre>
<p><strong>&#xFF08;2&#xFF09;&#x6A21;&#x5757;&#x7684;&#x6574;&#x4F53;&#x8F93;&#x5165;&#xFF0C;module&#x547D;&#x4EE4;</strong></p>
<p>export&#x547D;&#x4EE4;&#x9664;&#x4E86;&#x8F93;&#x51FA;&#x53D8;&#x91CF;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x8F93;&#x51FA;&#x65B9;&#x6CD5;&#x6216;&#x7C7B;&#xFF08;class&#xFF09;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;circle.js&#x6587;&#x4EF6;&#xFF0C;&#x5B83;&#x8F93;&#x51FA;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;area&#x548C;circumference&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// circle.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">area</span>(<span class="hljs-params">radius</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.PI * radius * radius;
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">circumference</span>(<span class="hljs-params">radius</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> * <span class="hljs-built_in">Math</span>.PI * radius;
}
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;main.js&#x8F93;&#x5165;circlek.js&#x6A21;&#x5757;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// main.js</span>

<span class="hljs-keyword">import</span> { area, circumference } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;circle&apos;</span>;

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5706;&#x9762;&#x79EF;&#xFF1A;&quot;</span> + area(<span class="hljs-number">4</span>));
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5706;&#x5468;&#x957F;&#xFF1A;&quot;</span> + circumference(<span class="hljs-number">14</span>));
</code></pre>
<p>&#x4E0A;&#x9762;&#x5199;&#x6CD5;&#x662F;&#x9010;&#x4E00;&#x6307;&#x5B9A;&#x8981;&#x8F93;&#x5165;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x53E6;&#x4E00;&#x79CD;&#x5199;&#x6CD5;&#x662F;&#x6574;&#x4F53;&#x8F93;&#x5165;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> circle <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;circle&apos;</span>;

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5706;&#x9762;&#x79EF;&#xFF1A;&quot;</span> + circle.area(<span class="hljs-number">4</span>));
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5706;&#x5468;&#x957F;&#xFF1A;&quot;</span> + circle.circumference(<span class="hljs-number">14</span>));
</code></pre>
<p>module&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x53D6;&#x4EE3;import&#x8BED;&#x53E5;&#xFF0C;&#x8FBE;&#x5230;&#x6574;&#x4F53;&#x8F93;&#x5165;&#x6A21;&#x5757;&#x7684;&#x4F5C;&#x7528;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// main.js</span>

<span class="hljs-built_in">module</span> circle from <span class="hljs-string">&apos;circle&apos;</span>;

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5706;&#x9762;&#x79EF;&#xFF1A;&quot;</span> + circle.area(<span class="hljs-number">4</span>));
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5706;&#x5468;&#x957F;&#xFF1A;&quot;</span> + circle.circumference(<span class="hljs-number">14</span>));
</code></pre>
<p>module&#x547D;&#x4EE4;&#x540E;&#x9762;&#x8DDF;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x8868;&#x793A;&#x8F93;&#x5165;&#x7684;&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x5728;&#x8BE5;&#x53D8;&#x91CF;&#x4E0A;&#x3002;</p>
<p><strong>&#xFF08;3&#xFF09;export default&#x547D;&#x4EE4;</strong></p>
<p>&#x5982;&#x679C;&#x60F3;&#x8981;&#x8F93;&#x51FA;&#x533F;&#x540D;&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;export default&#x547D;&#x4EE4;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// export-default.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;foo&apos;</span>);
}
</code></pre>
<p>&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x8F93;&#x5165;&#x8BE5;&#x6A21;&#x5757;&#x65F6;&#xFF0C;import&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x4E3A;&#x8BE5;&#x533F;&#x540D;&#x51FD;&#x6570;&#x6307;&#x5B9A;&#x4EFB;&#x610F;&#x540D;&#x5B57;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// import-default.js</span>

<span class="hljs-keyword">import</span> customName <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./export-default&apos;</span>;
customName(); <span class="hljs-comment">// &apos;foo&apos;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;import&#x547D;&#x4EE4;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4EFB;&#x610F;&#x540D;&#x79F0;&#x6307;&#x5411;&#x8F93;&#x51FA;&#x7684;&#x533F;&#x540D;&#x51FD;&#x6570;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x8FD9;&#x65F6;import&#x547D;&#x4EE4;&#x540E;&#x9762;&#xFF0C;&#x4E0D;&#x4F7F;&#x7528;&#x5927;&#x62EC;&#x53F7;&#x3002;</p>
<p>export default&#x547D;&#x4EE4;&#x7528;&#x5728;&#x975E;&#x533F;&#x540D;&#x51FD;&#x6570;&#x524D;&#xFF0C;&#x4E5F;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// export-default.js</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;foo&apos;</span>);
}

<span class="hljs-comment">// &#x6216;&#x8005;&#x5199;&#x6210;</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;foo&apos;</span>);
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> foo;
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;foo&#x51FD;&#x6570;&#x7684;&#x51FD;&#x6570;&#x540D;foo&#xFF0C;&#x5728;&#x6A21;&#x5757;&#x5916;&#x90E8;&#x662F;&#x65E0;&#x6548;&#x7684;&#x3002;&#x52A0;&#x8F7D;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x89C6;&#x540C;&#x533F;&#x540D;&#x51FD;&#x6570;&#x52A0;&#x8F7D;&#x3002;</p>
<p>export default&#x547D;&#x4EE4;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x6A21;&#x5757;&#x7684;&#x9ED8;&#x8BA4;&#x8F93;&#x51FA;&#x3002;&#x5982;&#x679C;&#x6A21;&#x5757;&#x52A0;&#x8F7D;&#x65F6;&#xFF0C;&#x53EA;&#x80FD;&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x503C;&#x6216;&#x65B9;&#x6CD5;&#xFF0C;&#x90A3;&#x5C31;&#x662F;export default&#x6240;&#x6307;&#x5B9A;&#x7684;&#x90A3;&#x4E2A;&#x503C;&#x6216;&#x65B9;&#x6CD5;&#x3002;&#x6240;&#x4EE5;&#xFF0C;import&#x547D;&#x4EE4;&#x540E;&#x9762;&#x624D;&#x4E0D;&#x7528;&#x52A0;&#x5927;&#x62EC;&#x53F7;&#x3002;&#x663E;&#x7136;&#xFF0C;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x8F93;&#x51FA;&#xFF0C;&#x56E0;&#x6B64;export deault&#x547D;&#x4EE4;&#x53EA;&#x80FD;&#x4F7F;&#x7528;&#x4E00;&#x6B21;&#x3002;</p>
<p>&#x6709;&#x4E86;export default&#x547D;&#x4EE4;&#xFF0C;&#x8F93;&#x5165;&#x6A21;&#x5757;&#x65F6;&#x5C31;&#x975E;&#x5E38;&#x76F4;&#x89C2;&#x4E86;&#xFF0C;&#x4EE5;&#x8F93;&#x5165;jQuery&#x6A21;&#x5757;&#x4E3A;&#x4F8B;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">import</span> $ from <span class="hljs-string">&apos;jquery&apos;</span>;
</code></pre>
<p>&#x5982;&#x679C;&#x60F3;&#x5728;&#x4E00;&#x6761;import&#x8BED;&#x53E5;&#x4E2D;&#xFF0C;&#x540C;&#x65F6;&#x8F93;&#x5165;&#x9ED8;&#x8BA4;&#x65B9;&#x6CD5;&#x548C;&#x5176;&#x4ED6;&#x53D8;&#x91CF;&#xFF0C;&#x53EF;&#x4EE5;&#x5199;&#x6210;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">import</span> customName, { otherMethod } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./export-default&apos;</span>;
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x8F93;&#x51FA;&#x9ED8;&#x8BA4;&#x7684;&#x503C;&#xFF0C;&#x53EA;&#x9700;&#x5C06;&#x503C;&#x8DDF;&#x5728;<code>export default</code>&#x4E4B;&#x540E;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-number">42</span>;
</code></pre>
<p>export default&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x8F93;&#x51FA;&#x7C7B;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// MyClass.js</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> </span>{ ... }

<span class="hljs-comment">// main.js</span>
<span class="hljs-keyword">import</span> MyClass <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;MyClass&apos;</span>
let o = new MyClass();
</code></pre>
<h2 id="%E6%A8%A1%E5%9D%97%E7%9A%84%E7%BB%A7%E6%89%BF">&#x6A21;&#x5757;&#x7684;&#x7EE7;&#x627F;</h2>
<p>&#x6A21;&#x5757;&#x4E4B;&#x95F4;&#x4E5F;&#x53EF;&#x4EE5;&#x7EE7;&#x627F;&#x3002;</p>
<p>&#x5047;&#x8BBE;&#x6709;&#x4E00;&#x4E2A;circleplus&#x6A21;&#x5757;&#xFF0C;&#x7EE7;&#x627F;&#x4E86;circle&#x6A21;&#x5757;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// circleplus.js</span>

<span class="hljs-keyword">export</span> * from <span class="hljs-string">&apos;circle&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">var</span> e = <span class="hljs-number">2.71828182846</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.exp(x);
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x7684;&#x201C;export *&#x201D;&#xFF0C;&#x8868;&#x793A;&#x8F93;&#x51FA;circle&#x6A21;&#x5757;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;export default&#x547D;&#x4EE4;&#x5B9A;&#x4E49;&#x6A21;&#x5757;&#x7684;&#x9ED8;&#x8BA4;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x8FD9;&#x65F6;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;circle&#x7684;&#x5C5E;&#x6027;&#x6216;&#x65B9;&#x6CD5;&#xFF0C;&#x6539;&#x540D;&#x540E;&#x518D;&#x8F93;&#x51FA;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// circleplus.js</span>

<span class="hljs-keyword">export</span> { area <span class="hljs-keyword">as</span> circleArea } from <span class="hljs-string">&apos;circle&apos;</span>;
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x8868;&#x793A;&#xFF0C;&#x53EA;&#x8F93;&#x51FA;circle&#x6A21;&#x5757;&#x7684;area&#x65B9;&#x6CD5;&#xFF0C;&#x4E14;&#x5C06;&#x5176;&#x6539;&#x540D;&#x4E3A;circleArea&#x3002;</p>
<p>&#x52A0;&#x8F7D;&#x4E0A;&#x9762;&#x6A21;&#x5757;&#x7684;&#x5199;&#x6CD5;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// main.js</span>

<span class="hljs-built_in">module</span> math from <span class="hljs-string">&quot;circleplus&quot;</span>;
<span class="hljs-keyword">import</span> exp <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;circleplus&quot;</span>;
<span class="hljs-built_in">console</span>.log(exp(math.pi));
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x7684;&quot;import exp&quot;&#x8868;&#x793A;&#xFF0C;&#x5C06;circleplus&#x6A21;&#x5757;&#x7684;&#x9ED8;&#x8BA4;&#x65B9;&#x6CD5;&#x52A0;&#x8F7D;&#x4E3A;exp&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="es6%E6%A8%A1%E5%9D%97%E7%9A%84%E8%BD%AC%E7%A0%81">ES6&#x6A21;&#x5757;&#x7684;&#x8F6C;&#x7801;</h2>
<p>&#x6D4F;&#x89C8;&#x5668;&#x76EE;&#x524D;&#x8FD8;&#x4E0D;&#x652F;&#x6301;ES6&#x6A21;&#x5757;&#xFF0C;&#x4E3A;&#x4E86;&#x73B0;&#x5728;&#x5C31;&#x80FD;&#x4F7F;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x8F6C;&#x4E3A;ES5&#x7684;&#x5199;&#x6CD5;&#x3002;</p>
<p><strong>&#xFF08;1&#xFF09;ES6 module transpiler</strong></p>
<p><a href="javascript:;" target="_blank">ES6 module transpiler</a>&#x662F;square&#x516C;&#x53F8;&#x5F00;&#x6E90;&#x7684;&#x4E00;&#x4E2A;&#x8F6C;&#x7801;&#x5668;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;ES6&#x6A21;&#x5757;&#x8F6C;&#x4E3A;CommonJS&#x6A21;&#x5757;&#x6216;AMD&#x6A21;&#x5757;&#x7684;&#x5199;&#x6CD5;&#xFF0C;&#x4ECE;&#x800C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x5B89;&#x88C5;&#x8FD9;&#x4E2A;&#x8F6C;&#x739B;&#x5668;&#x3002;</p>
<pre><code class="lang-bash">
$ npm install -g es6-module-transpiler
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x4F7F;&#x7528;<code>compile-modules convert</code>&#x547D;&#x4EE4;&#xFF0C;&#x5C06;ES6&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x8F6C;&#x7801;&#x3002;</p>
<pre><code class="lang-bash">
$ compile-modules convert file1.js file2.js
</code></pre>
<p>o&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x8F6C;&#x7801;&#x540E;&#x7684;&#x6587;&#x4EF6;&#x540D;&#x3002;</p>
<pre><code class="lang-bash">
$ compile-modules convert -o out.js file1.js
</code></pre>
<p><strong>&#xFF08;2&#xFF09;SystemJS</strong></p>
<p>&#x53E6;&#x4E00;&#x79CD;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528;<a href="javascript:;" target="_blank">SystemJS</a>&#x3002;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x57AB;&#x7247;&#x5E93;&#xFF08;polyfill&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x5185;&#x52A0;&#x8F7D;ES6&#x6A21;&#x5757;&#x3001;AMD&#x6A21;&#x5757;&#x548C;CommonJS&#x6A21;&#x5757;&#xFF0C;&#x5C06;&#x5176;&#x8F6C;&#x4E3A;ES5&#x683C;&#x5F0F;&#x3002;&#x5B83;&#x5728;&#x540E;&#x53F0;&#x8C03;&#x7528;&#x7684;&#x662F;Google&#x7684;Traceur&#x8F6C;&#x7801;&#x5668;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x5148;&#x5728;&#x7F51;&#x9875;&#x5185;&#x8F7D;&#x5165;system.js&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-html">
<span class="hljs-tag">&lt;<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">&quot;system.js&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x4F7F;&#x7528;<code>System.import</code>&#x65B9;&#x6CD5;&#x52A0;&#x8F7D;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-html">
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="actionscript">
  System.<span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./app&apos;</span>);
</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x7684;<code>./app</code>&#xFF0C;&#x6307;&#x7684;&#x662F;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x4E0B;&#x7684;app.js&#x6587;&#x4EF6;&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x662F;ES6&#x6A21;&#x5757;&#x6587;&#x4EF6;&#xFF0C;<code>System.import</code>&#x4F1A;&#x81EA;&#x52A8;&#x5C06;&#x5176;&#x8F6C;&#x7801;&#x3002;</p>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;<code>System.import</code>&#x4F7F;&#x7528;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x9488;&#x5BF9;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7F16;&#x7A0B;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// app/es6-file.js:</span>

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">q</span> </span>{
  constructor() {
    <span class="hljs-keyword">this</span>.es6 = <span class="hljs-string">&apos;hello&apos;</span>;
  }
}
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x5728;&#x7F51;&#x9875;&#x5185;&#x52A0;&#x8F7D;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-html">
<span class="hljs-tag">&lt;<span class="hljs-title">script</span>&gt;</span><span class="actionscript">

System.<span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;app/es6-file&apos;</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(m)</span> </span>{
  console.log(<span class="hljs-keyword">new</span> m.q().es6); <span class="hljs-comment">// hello</span>
});

</span><span class="hljs-tag">&lt;/<span class="hljs-title">script</span>&gt;</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>System.import</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x7528;then&#x65B9;&#x6CD5;&#x6307;&#x5B9A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="promise.html" class="navigation navigation-prev " aria-label="Previous page: Promise&#x5BF9;&#x8C61;"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="reference.html" class="navigation navigation-next " aria-label="Next page: &#x53C2;&#x8003;&#x94FE;&#x63A5;"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="static/js/app.js"></script>

    
    <script src="static/js/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"disqus":{"shortName":"git-book"}};
    gitbook.start(config);
});
</script>

        
    
    

</body></html>